import useDictStore from "@/store/modules/dict.js";
import {getDicts} from "@/api/system/dict/data.js";
import {getQueryData} from "@/api/tool/comQuery.js";

export function useDicts(..._keys) {
    
    let res = reactive({})
    _keys.forEach( dictType => {
        res[dictType] = []
        const dicts = useDictStore().getDict(dictType)
        if (dicts) {
            res[dictType] = dicts
        }
        else {
            if (dictType.startsWith("query")) {
                getQueryData(dictType).then((resp) => {
                    res[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
                    useDictStore().setDict(dictType, res[dictType]);
                })
            }
            else {
                getDicts(dictType).then(resp => {
                    res[dictType] = resp.data.map(p => ({ label: p.dictLabel, value: p.dictValue, elTagType: p.listClass, elTagClass: p.cssClass }))
                    useDictStore().setDict(dictType, res[dictType]);
                })
            }
        }
    } )
    return toRefs(res)
}
